package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.DepartmentDTO;
import com.hksj.Intelligentmanagement.entity.DepartmentEntity;
import com.hksj.Intelligentmanagement.mapper.DepartmentMapper;
import com.hksj.Intelligentmanagement.vo.AssistantVo;
import com.hksj.Intelligentmanagement.vo.DepartmentVo;
import com.hksj.Intelligentmanagement.vo.RoomVo;
import com.hksj.Intelligentmanagement.vo.SurgicalAssistantVo;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class DepartmentService extends ServiceImpl<DepartmentMapper, DepartmentEntity> {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private Mapper mapper;

    @Autowired
    private Gson gson;

    //科室新增
    public Result saveDepartment(DepartmentDTO departmentDTO) {

        DepartmentEntity departmentEntity =new DepartmentEntity();

        departmentEntity.setDepartmentName(departmentDTO.getDepartmentName());

        departmentEntity.setRegionId(departmentDTO.getRegionId());

        //默认科室状态是启用状态
        departmentEntity.setStatus("0");

        int i = departmentMapper.insert(departmentEntity);

        //新增失败的抛出异常
        if (i != 1) {
            throw new RuntimeException("新增失败");
        }

        log.info(gson.toJson(departmentEntity));

        return Result.ok("新增科室信息成功");
    }

    //科室删除
    public Result delDepartment(String id) {
        //根据区域id查询是否存在
        DepartmentEntity departmentEntity = departmentMapper.selectById(id);

        if (Objects.isNull(departmentEntity)){
            //不存在
            return Result.error("id输入有误,科室不存在");
        }
        //存在,逻辑删除
        //departmentMapper.deleteById(id);
        int i = departmentMapper.deleteById(departmentEntity);

        if (i != 1) {
            throw new RuntimeException("删除失败");
        }

        return Result.ok("删除科室信息成功");

    }

    //科室修改
    public Result updateDepartment(DepartmentDTO departmentDTO) {
        //根据id查询科室信息是否存在
        DepartmentEntity departmentEntity = departmentMapper.selectById(departmentDTO.getId());

        if (Objects.isNull(departmentEntity)) {
            //不存在
            return Result.error("输入的id有误,修改失败");
        }

        //存在
        //将departmentDTO转化为DepartmentEntity
        departmentEntity = mapper.map(departmentDTO, DepartmentEntity.class);

        //做更新
        int i = departmentMapper.updateById(departmentEntity);

        if (i != 1) {
            throw new RuntimeException("更新失败");
        }
        return Result.ok("更新科室信息成功");

    }

    //根据id查询科室信息
    public Result queryDepartmentId(String id) {
        //根据区域id查询是否存在
        DepartmentEntity departmentEntity = departmentMapper.selectById(id);

        if (Objects.isNull(departmentEntity)){
            //不存在
            return Result.error("id输入有误,查询科室信息失败");
        }

        return Result.ok(departmentEntity);
    }

    //根据区域id查询对应所有科室 分页显示
    public IPage<DepartmentVo> queryDepartmentListByPage(DepartmentDTO departmentDTO) {

        //创建一个分页实例
        IPage<DepartmentVo> page = new Page<>(departmentDTO.getPageNum(), departmentDTO.getPageSize());

        //分页查询
        List<DepartmentVo> departmentVoList = departmentMapper.queryDepartmentListByPage(page, departmentDTO);

        //将分页结果设置到分页中
        page.setRecords(departmentVoList);

        //返回分页
        return page;
    }

    public List<RoomVo> listRoomByRegionId(String regionId, String time, Integer type) {
        List<RoomVo> roomVos = departmentMapper.listRoomByRegionId(regionId, time,type);
        for (RoomVo roomVo : roomVos) {
            for (SurgicalAssistantVo surgicalAssistantVo : roomVo.getSurgicalList()) {
                for (AssistantVo assistantVo : surgicalAssistantVo.getAssistantList()) {
                    switch (assistantVo.getType()){
                        case 0:
                            //主麻
                            surgicalAssistantVo.getAnesthesiaList().add(assistantVo);
                            break;
                        case 1:
                            //副麻
                            surgicalAssistantVo.getSecondAnesthesiaList().add(assistantVo);
                            break;
                        case 2:
                            //麻助
                            surgicalAssistantVo.getAnesthesiaAssistantList().add(assistantVo);
                            break;
                        case 3:
                            //洗手护士
                            surgicalAssistantVo.getWashNurseList().add(assistantVo);
                            break;
                        case 4:
                            //巡回护士
                            surgicalAssistantVo.getTourNurseList().add(assistantVo);
                            break;
                        case 5:
                            //护助
                            surgicalAssistantVo.getNurseAssistantList().add(assistantVo);
                            break;
                        case 6:
                            //麻醉指导
                            surgicalAssistantVo.getAnesthesiaInstructorList().add(assistantVo);
                            break;
                    }
                }
            }
            roomVo.setNum(roomVo.getSurgicalList().size());
        }
        return roomVos;
    }
}
